import {findAllCoffeeList, findCoffeeByName} from './service'
export default {
    namespace: 'index',
    state: {
      isInited: false,
      coffee: {},
      coffeeList:[],
      testtest:'testtest'
    },
    effects: {
        *init({ payload }, { call, put }){
          const resp = yield call(findAllCoffeeList);
          console.log(resp);
          if(resp.status == 200){
            yield put({
              type: 'update',
              payload: {coffeeList:resp.data, isInited:true},//想改state中的xxx，就{xxx:resp.data}
            });
          } 
        },
        *testCoffee({ payload }, { call, put }) {
          try {
            console.log(payload);
            const resp = yield call(findCoffeeByName, payload);
            console.log(resp);
            if(resp.status == 200){
              yield put({
                type: 'update',
                payload: {coffee:resp.data},//想改state中的xxx，就{xxx:resp.data}
              });
            } 
            // localStorage.setItem('userinfo', JSON.stringify(userinfo));
            // 触发reducers更新状态
            // 犹如import { call, put, takeEvery } from 'redux-saga/effects'中的takeEvery
       
            // history.push('/');
          } catch (error) {
            console.log(error);
          }
        },
    },
    reducers: {
      // getCoffee(state, { payload: id }) {
      //   return state.filter(item => item.id !== id);
      // },
      update(state, action) {
        //获得传入参数，action.payload。将传参与对应的state进行合并
        // 将状态更新到state
        console.log('reducers-update',action.payload);
        return {...state, ...action.payload};
      }
    },
  };